import { BrowserWindow, Protocol } from 'electron'
const remote: any = window.require('electron').remote
const win: BrowserWindow = remote.getCurrentWindow()
const protocol: Protocol = remote.protocol

console.log('注册')

for (const scheme of ['zchrome-search', 'zchrome']) {
  protocol.unregisterProtocol(scheme)
  protocol.registerHttpProtocol(
    scheme,
    (request, respond) => {
      console.log('url' + request.url)
      const url = new URL(scheme + new URL(request.url).pathname.substr(1), win.webContents.getURL()).href
      console.log('请求', url)
      respond({ url })
    },
    error => {
      if (error) {
        console.error(`Failed to register ${scheme} protocol`, error)
      }
    }
  )
}
